package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CoursesDao;
import entity.Courses;

/**
 * Servlet implementation class GetCourses
 */
@WebServlet("/GetCourses")
public class GetCourses extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GetCourses() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		CoursesDao cd = new CoursesDao();
		Integer course_id = Integer.valueOf(request.getParameter("mname"));
		List<Courses> list = cd.getCourses(course_id);
		String cs = "[";
		for (Courses c : list) {
			cs += "{\"id\":" + c.getId() + ",\"name\":\"" + c.getName() + "\"},";
		}
		cs = cs.substring(0, cs.length() - 1);
		cs += "]";
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		out.println(cs);
//		System.out.println("123" + cs);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
